package com.shanghaionstar.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.vehicle.response.RemoteHistoryEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.user.adapter.RemoteHistoryAdapter;
import com.shanghaionstar.user.iview.IRemoteControlHistoryView;
import com.shanghaionstar.user.presenter.RemoteControlHistoryPresenter;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/12.
 */
public class RemoteControlHistoryFragment extends BaseFragment implements IRemoteControlHistoryView {

    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private ProgressLayout layout_progress;
    @FindViewById
    private CommonNoDatasLayout no_datas_layout;
    @FindViewById
    private ListView history_list;
    private RemoteControlHistoryPresenter presenter;
    private MoreActivity parentActivity;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnBackClick(top_layout);
        if (isFirstLoad()) {
            parentActivity = (MoreActivity) getActivity();
            presenter = new RemoteControlHistoryPresenter(parentActivity.getScheduler());
            presenter.setView(this);
            presenter.start();
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.more_remote_control_history;
    }

    @Override
    public void setLoading(boolean isLoading) {
        layout_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        no_datas_layout.setHintText(R.string.error_remote_control_history);
        history_list.setVisibility(View.GONE);
    }

    @Override
    public void onReponseSuccess(RemoteHistoryEntity object) {
        if (object != null && object.getCommandResponses() != null && object.getCommandResponses().getCommandResponse() != null && object.getCommandResponses().getCommandResponse().size() > 0) {
            history_list.setAdapter(new RemoteHistoryAdapter(object.getCommandResponses().getCommandResponse(), onActivity()));
            no_datas_layout.setVisibility(View.GONE);
            history_list.setVisibility(View.VISIBLE);
        } else {
            no_datas_layout.setHintText(R.string.no_remote_control_history);
            history_list.setVisibility(View.GONE);
        }
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
